package com.wufun.overseas.utils;

import android.graphics.Color;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.View;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created by xiet on 2015/8/11.
 */
public class HtmlDocUtil {
    public static class PostDetail {
        public enum Type {
            Text,
            Image,
            Video,
        }

        public Type   type;
        public Object content;

        public Type getType() {
            return type;
        }

        public void setType(Type type) {
            this.type = type;
        }

        public Object getContent() {
            return content;
        }

        public void setContent(Object content) {
            this.content = content;
        }

        public PostDetail() {
        }
    }

    public static class PostText extends PostDetail {
        //        public String                 text;
        public SpannableStringBuilder text;

//        public PostText(String text) {
//            this.text = text;
//        }


        public PostText(SpannableStringBuilder text) {
            this.text = text;
        }

        public PostText() {
        }

//        public String getText() {
//            return text;
//        }
//
//        public void setText(String text) {
//            this.text = text;
//        }

        public SpannableStringBuilder getText() {
            return text;
        }

        public void setText(SpannableStringBuilder text) {
            this.text = text;
        }
    }

    public static class PostImage extends PostDetail {
        public String imgUrl;
        public String imgUrlLarge;

        public PostImage() {
        }

        public PostImage(String imgUrl, String imgUrlLarge) {
            this.imgUrl = imgUrl;
            this.imgUrlLarge = imgUrlLarge;
        }

        public String getImgUrl() {
            return imgUrl;
        }

        public void setImgUrl(String imgUrl) {
            this.imgUrl = imgUrl;
        }

        public String getImgUrlLarge() {
            return imgUrlLarge;
        }

        public void setImgUrlLarge(String imgUrlLarge) {
            this.imgUrlLarge = imgUrlLarge;
        }
    }

    public static class PostVideo extends PostDetail {
        public String imgUrl;
        public String videoUrl;

        public PostVideo() {
        }

        public PostVideo(String imgUrl, String videoUrl) {
            this.imgUrl = imgUrl;
            this.videoUrl = videoUrl;
        }

        public String getImgUrl() {
            return imgUrl;
        }

        public void setImgUrl(String imgUrl) {
            this.imgUrl = imgUrl;
        }

        public String getVideoUrl() {
            return videoUrl;
        }

        public void setVideoUrl(String videoUrl) {
            this.videoUrl = videoUrl;
        }
    }

    public static boolean isPostDoc(String str) {
        boolean ret = false;
        if (!StringUtils.isEmpty(str)) {
            try {
                Document doc = Jsoup.parse(str);

                ret = doc != null && doc.getElementsByClass("detail") != null && doc.getElementsByClass("detail").size() != 0;
            } catch (Exception e) {
                e.printStackTrace();
                ret = false;
            }
        }

        return ret;
    }

    public static List<PostDetail> getPostDetail(String str) {
        List<PostDetail> ret = new ArrayList<PostDetail>();

        Document doc = Jsoup.parse(str);

        Element detail = doc.getElementsByClass("detail").get(0);
//        detail.select("script").remove();

        Elements childrenEle = detail.children();
        for (Element element : childrenEle) {
            PostDetail postDetail = new PostDetail();
            String nodeName   = element.nodeName();
            if (nodeName.equals("p")) {

                List<Node> childNodes = element.childNodes();
                for (int i = 0; i < childNodes.size(); i++) {
                    postDetail = new PostDetail();
                    Node node = childNodes.get(i);

                    if (node instanceof TextNode) {
                        String s = ((TextNode) node).text();
                        if (ret != null && ret.size() > 0) {
                            PostDetail lastDetail = ret.get(ret.size() - 1);
                            if (lastDetail.getType() == PostDetail.Type.Text && i != 0) {
                                SpannableStringBuilder lastDetailStr = ((PostText) lastDetail.getContent()).getText();
                                lastDetailStr.append(s);
//                                lastDetail.setContent(new PostText(lastDetailStr));
                                continue;
                            }
                        }
                        postDetail.setType(PostDetail.Type.Text);
                        postDetail.setContent(new PostText(new SpannableStringBuilder().append(s)));
                        ret.add(postDetail);
                    } else if (node instanceof Element) {
                        if (node.nodeName().equals("img")) {
                            if (!StringUtils.isEmpty(node.attr("uurl"))) {
                                postDetail.setType(PostDetail.Type.Video);
                                postDetail.setContent(new PostVideo(node.attr("src"), node.attr("uurl")));
                            } else {
                                postDetail.setType(PostDetail.Type.Image);
                                postDetail.setContent(new PostImage(node.attr("src"), node.attr("bsrc")));
                            }

                            ret.add(postDetail);
                        } else if (node.nodeName().equals("br")) {
                            if (ret != null && ret.size() > 0) {
                                PostDetail lastDetail = ret.get(ret.size() - 1);
                                if (lastDetail.getType() == PostDetail.Type.Text
                                        && i != 0) {
                                    SpannableStringBuilder lastDetailStr = ((PostText) lastDetail.getContent()).getText();
                                    lastDetailStr.append("\n");
                                    lastDetail.setContent(new PostText(lastDetailStr));
                                    continue;
                                }
                            }
                            postDetail.setType(PostDetail.Type.Text);
                            postDetail.setContent(new PostText(new SpannableStringBuilder().append(" ")));
                            ret.add(postDetail);
                        } else if (node.nodeName().equals("a")) {
                            String href = node.attr("href");
                            if (StringUtils.isEmpty(href)) href = "#";
                            String s  = ((Element) node).text();
                            SpannableString ss = new SpannableString(s);
                            ss.setSpan(new LinkClickSpan(href), 0, ss.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

                            if (ret != null && ret.size() > 0) {
                                PostDetail lastDetail = ret.get(ret.size() - 1);
                                if (lastDetail.getType() == PostDetail.Type.Text && i != 0) {
                                    SpannableStringBuilder lastDetailStr = ((PostText) lastDetail.getContent()).getText();
                                    lastDetailStr.append(ss);
//                                lastDetail.setContent(new PostText(lastDetailStr));
                                    continue;
                                }
                            }
                            postDetail.setType(PostDetail.Type.Text);
                            postDetail.setContent(new PostText(new SpannableStringBuilder().append(ss)));
                            ret.add(postDetail);
                        }
                    }

                }
            } else if (nodeName.equals("img")) {
                if (!StringUtils.isEmpty(element.attr("uurl"))) {
                    postDetail.setType(PostDetail.Type.Video);
                    postDetail.setContent(new PostVideo(element.attr("src"), element.attr("uurl")));
                } else {
                    postDetail.setType(PostDetail.Type.Image);
                    postDetail.setContent(new PostImage(element.attr("src"), element.attr("bsrc")));
                }

                ret.add(postDetail);
            } else if (element.nodeName().equals("a")) {
                String href = element.attr("href");
                if (StringUtils.isEmpty(href)) href = "#";
                String s  = element.text();
                SpannableString ss = new SpannableString(s);
                ss.setSpan(new LinkClickSpan(href), 0, ss.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

//                if (ret != null && ret.size() > 0) {
//                    PostDetail lastDetail = ret.get(ret.size() - 1);
//                    if (lastDetail.getType() == PostDetail.Type.Text && i != 0) {
//                        SpannableStringBuilder lastDetailStr = ((PostText) lastDetail.getContent()).getText();
//                        lastDetailStr.append(ss);
////                                lastDetail.setContent(new PostText(lastDetailStr));
//                        continue;
//                    }
//                }
                postDetail.setType(PostDetail.Type.Text);
                postDetail.setContent(new PostText(new SpannableStringBuilder().append(ss)));
                ret.add(postDetail);
            }

        }
        return ret;
    }

    private static class LinkClickSpan extends ClickableSpan {
        public LinkClickSpan() {
        }

        private String href;

        public LinkClickSpan(String href) {
            this.href = href;
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(ds.linkColor);
            ds.bgColor = Color.parseColor("#00000000");
            ds.setUnderlineText(false); //去掉下划线
            ds.clearShadowLayer();
        }

        @Override
        public void onClick(View widget) {
//            IntentUtil.getInstance().goShareWebActivity(widget.getContext(), href);
        }
    }

    public static String ToDBC(String input) {
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {
                c[i] = (char) 32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375)
                c[i] = (char) (c[i] - 65248);
        }
        return new String(c);
    }

    public static String getDate(String str) {
        Date date = new Date(Long.parseLong(str));
        SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        return sdf.format(date);
    }
}
